<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;
//
class RunmenanalysisController extends AdminbaseController {
    
    function _initialize()
    {
        parent::_initialize();
    }
   
    /**
     * 跑客信息
     */
    function dispatch_message(){
        $dispatch = M('dispatch');
        //*******************************************************
        $formget = I('formget');
        $company_id = intval($formget["company_id"]);//所属团队
       // $company_branch = intval($formget["company_branch"]);//所属团队公司
        $dispatch_name = $formget["dispatch_name"];//跑客姓名
        $mobile =$formget["mobile"];//跑客手机号
        $is_sign= intval($formget["is_sign"]);//跑客状态
        $sign_time=  $formget["sign_time"];//签约时间


        $formget =array('company_id'=>$company_id,
            'dispatch_name'=>$dispatch_name,
            'is_sign'=>$is_sign,
            'mobile'=>$mobile,
            'sign_time'=>$sign_time);

        $where = " 1=1 ";
        if($company_id!=1)
            $where = $where." and company_id = ".$company_id;
        if(!empty($dispatch_name))
            $where = $where." and dispatch_name like '%".$dispatch_name."%'";
        if(!empty($mobile))
            $where = $where.' and mobile = '.$mobile;
        if($is_sign!=-1)
            $where = $where.' and is_sign = '.$is_sign;
        if(!empty($sign_time))
            $where = $where." and sign_time like '".$sign_time."%'" ;

        $count = $dispatch->where($where)->count();
        //echo $dispatch->getLastSql();
        import("Org.Util.Pagemumayi");
        $page = new \Pagemumayi($count,20);
        //分页跳转的时候保证查询条件
        foreach($formget as $key=>$val) {
            $page->parameter .= 'formget['."$key"."]=".urlencode($val)."&";
        }

        $lists = $dispatch
            ->field('company_branch,company_id,dispatch_name,mobile,cert_number,avatar,is_sign,sign_time')
            ->where($where)
            ->order("sign_time desc")
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
       // echo $dispatch->getLastSql();
        $this->assign('total_count', $count);
        $this->assign("formget",$formget);
        $this->assign('lists', $lists);
        $this->assign("page", $page->show('Admin'));
        $this->display();

    }
    
    /**
     *  跑单信息
     */
    function  dispatch_order(){
        $dispatch = M('dispatch d');
        $order = M('order o');
        
        $formget = I('formget');
        $company_id = intval($formget["company_id"]);//所属团队
        $dispatch_name = $formget["dispatch_name"];//跑客姓名
        $mobile = $formget["mobile"];//跑客手机号
        $start_time = $formget['start_time'];
        $end_time = $formget['end_time'];  //时段
        
        if(empty($start_time))
            $start_time = date('Y-m-d',time());
        if(empty($end_time))
            $end_time = date('Y-m-d',time());
        
        $formget =array('company_id'=>$company_id,
            'dispatch_name'=>$dispatch_name,
            'mobile'=>$mobile,
            'start_time'=>$start_time,
            'end_time'=>$end_time);
        $where = " o.pay_status=1 and o.status =7 and d.is_sign = 2 ";
        $where = $where." and o.finish_time >= '".$start_time." 00:00:00'";
        $where = $where." and o.finish_time <= '".$end_time." 23:59:59' ";
        //dump($dispatch_name);
        if($company_id!=1)
            $where = $where." and d.company_id = ".$company_id;
        if(!empty($dispatch_name))
            $where = $where." and dispatch_name like '%".$dispatch_name."%'";
        if(!empty($mobile))
            $where = $where.' and mobile = '.$mobile;
        
        $sql_count = "select count(*) as tp_count from (select d.dispatch_id from db_dispatch as d LEFT JOIN db_order as o on d.dispatch_id = o.dispatch_id where ".$where." GROUP BY(d.dispatch_id)) as tablename2";
       //dump($sql_count);
        $count = M()->query($sql_count);   
        $count = $count[0]["tp_count"];

        import("Org.Util.Pagemumayi");
        $page = new \Pagemumayi($count,20);
        //分页跳转的时候保证查询条件
        foreach($formget as $key=>$val) {
            $page->parameter .= 'formget['."$key"."]=".urlencode($val)."&";
        }


//        $sql ="select d.dispatch_id,d.company_branch,d.company_id,d.dispatch_name,d.mobile,d.sign_time,d.comp_order_num,sum(order_amount) as order_amount from db_dispatch as d LEFT JOIN db_order as o on d.dispatch_id = o.dispatch_id where o.pay_status=1 and o.status =7 and d.is_sign = 2 ".$where." GROUP BY(d.dispatch_id)";

        $dispatch = M('dispatch d');
     $lists = $dispatch
            ->field(' d.dispatch_id,d.company_branch,d.company_id,d.dispatch_name,d.mobile,d.sign_time,count(o.order_id) as comp_order_num,sum(order_amount) as order_amount')
            ->join('db_order as o on d.dispatch_id = o.dispatch_id')
            ->where($where)
            ->group("d.dispatch_id")
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
    // echo $dispatch->getLastSql();
        $this->assign('total_count', $count);
        $this->assign("formget",$formget);
        $this->assign('lists', $lists);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

    /**
     *  推荐人信息
     */
    function recom_analysis_list()
    {

        $water_db = M('recommend r');

        $formget = I('formget');
        $recommended_type = $formget["recommended_type"];//被推荐人类别
        $recommended_name = $formget["recommended_name"];//被推荐人姓名
        $recommended_mobile = $formget["recommended_mobile"];//被推荐人手机
        $recom_name = $formget["recom_name"];//推荐人手机
        $recom_mobile = $formget["recom_mobile"];//推荐人手机
        $recom_type = $formget['recom_type'];//推荐人类型

        $start_time = $formget["start_time"];//开始时间
        $end_time = $formget["end_time"];//结束时间

        $formget = array('recommended_type' => $recommended_type
                            ,'recommended_name' => $recommended_name
                            ,'recommended_mobile' => $recommended_mobile
                             , 'recom_type' => $recom_type
                            , 'recom_name' => $recom_name
                            , 'recom_mobile' => $recom_mobile
                            , 'start_time' => $start_time
                            , 'end_time' => $end_time);

        $where = " 1=1 ";

        //被推荐人
            if ($recommended_type == '1'){
                if($recommended_mobile)
                    $where = $where . " and a2.mobile like '%" . $recommended_mobile . "%'";
                if($recommended_name)
                    $where = $where . " and a2.mobile like '%" . $recommended_name . "%'";
                $where = $where . " and r.recommended_person_type =" . $recommended_type;
            }else if ($recommended_type == '2'){
                if($recommended_mobile)
                    $where = $where . " and d2.mobile like '%" . $recommended_mobile . "%'";
                if($recommended_name)
                    $where = $where . " and d2.dispatch_name like '%" . $recommended_name . "%'";
                $where = $where . " and r.recommended_person_type =" . $recommended_type;
            }

        //推荐人姓名查询
        if ($recom_type == '1'){
            if($recom_name)
                $where = $where . " and a1.mobile like '%" . $recom_name . "%'";
            if($recom_mobile)
                $where = $where . " and a1.mobile like '%" . $recom_mobile . "%'";

            $where = $where . " and r.recom_person_type =" . $recom_type;
        }else if ($recom_type == '2'){
            if($recom_name)
                $where = $where . " and d1.dispatch_name like '%" . $recom_name . "%'";
            if($recom_mobile)
                $where = $where . " and d1.mobile like '%" . $recom_mobile . "%'";

            $where = $where . " and r.recom_person_type =" . $recom_type;
        }

        if ($start_time)
            $where = $where . " and r.recommended_time >= '" . $start_time . " 00:00:00'";
        if ($end_time)
            $where = $where . " and r.recommended_time <= '" . $end_time . " 23:59:59' ";

        $count = $water_db->where($where)->join('left  join db_app_users a1 on r.recom_person_id = a1.user_id and r.recom_person_type=1')
            ->join('left join db_dispatch d1 on r.recom_person_id = d1.dispatch_id and r.recom_person_type =2')
            ->join('left join db_app_users a2 on a2.user_id = r.recommended_person_id and r.recommended_person_type = 1')
            ->join('left join db_dispatch d2 on d2.dispatch_id = r.recommended_person_id and r.recommended_person_type=2')
            ->count();//left join 不影响记录数，但包含查询条件
        import("Org.Util.Pagemumayi");
        $page = new \Pagemumayi($count, 20);
        //分页跳转的时候保证查询条件
        foreach($formget as $key=>$val) {
            $page->parameter .= 'formget['."$key"."]=".urlencode($val)."&";
        }

        $data = $water_db->field('case when r.recommended_person_type = 1 then a2.mobile' .
            ' when r.recommended_person_type = 2 then ifnull(d2.dispatch_name,d2.mobile) end as recommend_name' .
            ',case when r.recommended_person_type = 1 then a2.mobile ' .
            ' when r.recommended_person_type = 2 then d2.mobile end  as recommend_mobile'.
            ',case when r.recom_person_type = 1 then a1.mobile' .
            ' when r.recom_person_type = 2 then ifnull(d1.dispatch_name,d1.mobile) end  as recom_name' .
            ',case when r.recom_person_type = 1 then a1.mobile' .
            ' when r.recom_person_type = 2 then d1.mobile end  as recom_mobile,r.*')
            ->join('left  join db_app_users a1 on r.recom_person_id = a1.user_id and r.recom_person_type=1')  //推荐人
            ->join('left join db_dispatch d1 on r.recom_person_id = d1.dispatch_id and r.recom_person_type =2')
            ->join('left join db_app_users a2 on a2.user_id = r.recommended_person_id and r.recommended_person_type = 1')//被推荐人
            ->join('left join db_dispatch d2 on d2.dispatch_id = r.recommended_person_id and r.recommended_person_type=2')
            ->where($where)
            ->order('r.recommended_time desc')
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();

        $this->assign('datas', $data);
        $this->assign('total_count', $count);
        $this->assign('page', $page->show('Admin'));
        $this->assign("formget", $formget);
        $this->display();
    }
    /**
     * 听单信息
     */
    function pn_work_list(){

        $formget = I('formget');

        $dispatch_name=  $formget["dispatch_name"];//跑客姓名
        $mobile=  $formget["mobile"];//电话
        $company_id=  intval($formget["company_id"]);//团队

        $work_status=  $formget["work_status"];//开始时间

        $formget=array('dispatch_name'=>$dispatch_name
        ,'mobile'=>$mobile,
            'company_id'=>$company_id,
            'work_status'=>$work_status
        );

        $where = " ";

        if( $dispatch_name)
            $where = $where." and dispatch_name like '%".$dispatch_name."%'";
        if( $mobile)
            $where = $where." and mobile like '%".$mobile."%'";
        if($company_id!=1 && $company_id != 0)
            $where = $where.' and company_id = '.$company_id;

        if($work_status!=-1 && $work_status !='')
            $where = $where.' and work_status = '.$work_status;

        $dispatch_db =  M('dispatch d ');
        $count = $dispatch_db->where(' is_sign = 2 '.$where)->count();

        import("Org.Util.Pagemumayi");
        $page = new \Pagemumayi($count,20);
        //分页跳转的时候保证查询条件
        foreach($formget as $key=>$val) {
            $page->parameter .= 'formget['."$key"."]=".urlencode($val)."&";
        }

        $datas = $dispatch_db->field('d.dispatch_id,d.company_id,d.company_branch,d.dispatch_name,d.mobile,d.work_status,d.is_accept_dispatch,d.onlinetime ,d.todaytime')
            ->where(' is_sign = 2 '.$where)->order('d.company_id')
            ->limit($page->firstRow . ',' . $page->listRows)->select();

        $this->assign("lists", $datas);
        $this->assign("total_count",$count);
        $this->assign("formget",$formget);
        $this->assign("page", $page->show('Admin'));
        $this->assign('company_data',  M('company')->select());
        $this->display();
    }
    /**
     * 跑客分布--初始页面
     *  order_id 订单id
     */
    function allpnroutecounts(){
        $this->display();
    }

    /**
     * 跑客分布--异步请求跑客信息
     *  order_id 订单id
     */
    function allpnroute(){
        $company = M("company");
        $order_resmsm_db = M('order_resmsm r');
        $Model = M();
        $rets = $company->select();//公司信息

        $ret_data = array();
        $count=0;
        foreach ( $rets as $key => $value){
            //一个团队的签约的听单中的距离当天在线的跑客
            $retd = "select dispatch_id as id,latitude as lat,longitude as lon,"
                . "dispatch_name,place_up_time,mobile from db_dispatch where "
                . " work_status =1 and is_sign=2 and company_id=".$value['company_id'] ." and DATE_FORMAT(place_up_time,'%Y-%m-%d') = DATE_FORMAT(now(),'%Y-%m-%d')";//把已签约正在听单中的跑客根据compan_id分类找出来

            $ret_data['data'][$key]['company_id']=$value['company_id'];
            $ret_data['data'][$key]['company_img']=$value['icon'];
            $ret_data['data'][$key]['data'] = $Model->query($retd);

            if($ret_data['data'][$key]['data'] )
            {
                foreach($ret_data['data'][$key]['data'] as $k => $v)
                {
                    $dispatch_id = $v['id'];
                    $where = "dispatch_id = $dispatch_id  &&  pay_status=1 && status>1";
                    $ret_data['data'][$key]['data'][$k]['order_num'] = $order_resmsm_db
                        ->where($where)
                        ->count();//当前此跑客未完成的订单记录数

                    $ret_data['data'][$key]['data'][$k]['poisition']= $order_resmsm_db->field('order_data')->where($where)
                        ->where($where)
                        ->select();
                    if($ret_data['data'][$key]['data'][$k]['poisition'])//获取订单的起始位置信息
                    {
                        for($j=0;$j<count($ret_data['data'][$key]['data'][$k]['poisition']);$j++)
                        {
                            $order_data= json_decode($ret_data['data'][$key]['data'][$k]['poisition'][$j]["order_data"], true);
                            $ret_data['data'][$key]['data'][$k]['poisition'][$j]['send_longit'] = $order_data['send_longit'];
                            $ret_data['data'][$key]['data'][$k]['poisition'][$j]['send_latit'] = $order_data['send_latit'];
                            $ret_data['data'][$key]['data'][$k]['poisition'][$j]['reci_longit'] = $order_data['reci_longit'];
                            $ret_data['data'][$key]['data'][$k]['poisition'][$j]['reci_latit'] = $order_data['reci_latit'];
                        }
                    }
                }
                $count = $count + count($ret_data['data'][$key]['data']);
            }
        }

        $ret_data['count']=$count;
        if($ret_data){
            echo json_encode($ret_data);
        }

    }

    /**
     * 跑客分布--异步请求跑客信息
     *  order_id 订单id
     */
    function pn_online_list($name,$mobile){
        $model = M('dispatch d ');
        $where_data = array("work_status"=>1
                                ,"is_sign"=>2
                                ,"DATE_FORMAT(place_up_time,'%Y-%m-%d')"=>date('Y-m-d',time())
                        );

        if($name)
            $where_data['dispatch_name']=array('like','%'.$name.'%');
        if($mobile)
            $where_data['mobile']=array('like','%'.$mobile.'%');

        //一个团队的签约的听单中的距离当天在线的跑客
        $ret_data = $model->field('dispatch_id as id,latitude as lat,longitude as lon,dispatch_name,place_up_time,mobile,company_id')
                            ->where($where_data)
                            ->order('company_id,place_up_time desc')
                          ->select();

        $this->assign('lists',  $ret_data);
        $this->display();

    }
    
        /**
     * 发单信息
     */
    function order_message(){
        $formget = I('formget');
        $shop_name = $formget['shop_name']; //名称
        $start_time = $formget['start_time']; //时段
        $end_time = $formget['end_time'];
        //dump($start_time);
        
        if(empty($start_time))
            $start_time = date('Y-m-d',time());
        if(empty($end_time))
            $end_time = date('Y-m-d',time());
        
        $formget = array('start_time'=>$start_time,
                        'end_time'=>$end_time,
                        'shop_name'=>$shop_name);
        
        $where = ' where  o.`status`=7 ';
        
        $where = $where." and o.order_time >= '".$start_time." 00:00:00'";
        $where = $where." and o.order_time <= '".$end_time." 23:59:59' ";
        //dump($where);
        $user_where = ' where 1 ' ;
        if(!empty($shop_name))
            $user_where = $user_where." and s.shop_name like '%".$shop_name."%'";
        
        $shop_sql = "select s.shop_name".
                    " ,s.shop_area".
                    "   ,a.mobile".
                    "   ,IF(order_src.order_counts IS NULL, 0, order_src.order_counts) as order_counts ".
                    "  ,IF(order_src.order_amount IS NULL, 0.00, order_src.order_amount) as order_amount".
                    "  ,IF(order_src.real_amount IS NULL, 0.00, order_src.real_amount) as real_amount".
                    "    ,order_src.order_time".
                    "  from db_user_shop s ".
                    "  left join db_app_users a on s.shop_id = a.is_shop".
                    "   left join ".
                    "   ( select user_id,count(order_id) as order_counts,sum(order_amount) as order_amount ,sum(o.real_amount) as real_amount , DATE_FORMAT(o.order_time,'%Y-%m') as order_time from db_order o ".
                            $where.
                    "       GROUP BY user_id".
                    "     )as order_src on order_src.user_id = a.user_id ".
                        $user_where.
                    "     order by s.shop_area,s.sort,s.time";

        $data = M()->query($shop_sql);
//        dump($shop_sql);
//        die;
        $this->assign("formget",$formget);
        $this->assign('lists',$data);
        
        $this->display();
    }
   
/**
     * 订单信息
     */
    function order_detail(){
        $dispatch = M('dispatch d');
        $order = M('order o');
        $app_users = M('app_users a');
        
        $formget = I('formget');
        $order_number = $formget['order_number']; //订单号
        $order_time = $formget['order_time']; //时间
        $order_type = intval($formget['order_type']); //类型
        $is_reservation = intval($formget['is_reservation']); //预约
        $user_mobile = $formget['user_mobile']; //用户电话
        $dispatch_name = $formget['dispatch_name']; //跑客姓名
        $dispatch_mobile = $formget['dispatch_mobile']; //跑客电话
        
        $formget = array('order_number'    =>$order_number,
                         'order_time'      =>$order_time,
                         'order_type'      =>$order_type,
                         'is_reservation'  =>$is_reservation,
                         'user_mobile'     =>$user_mobile,
                         'dispatch_name' =>$dispatch_name,
                         'dispatch_mobile' =>$dispatch_mobile);
//        if(empty($order_time))
//            $order_time = date('Y-m-d',time());
        
        $where = "1=1";
        if(!empty($order_number))
            $where = $where." and order_number ='".$order_number."'";
        if(!empty($order_time))
            $where = $where. " and order_time like '".$order_time."%'" ;
        if($order_type!= -1)
            $where = $where." and order_type = ".$order_type;
        if($is_reservation!=-1)
            $where = $where." and is_reservation = ".$is_reservation;
        if(!empty($user_mobile))
            $where = $where." and a.mobile = ".$user_mobile;
        if(!empty($dispatch_name))
            $where = $where." and dispatch_name like '".$dispatch_name."%'"; 
        if(!empty ($dispatch_mobile))   
            $where = $where." and d.mobile = ".$dispatch_mobile;
        
        $count = $order->join('db_app_users as a on a.user_id = o.user_id')
                ->join('db_dispatch as d on d.dispatch_id = o.dispatch_id')
                ->where($where)
                ->count();
         import("Org.Util.Pagemumayi");
         $page = new \Pagemumayi($count,20);
        //分页跳转的时候保证查询条件
         foreach($formget as $key=>$val) {
            $page->parameter .= 'formget['."$key"."]=".urlencode($val)."&";
        }
        
        $lists = $order
            ->field('order_number,order_time,order_type,status,'
                    . 'pay_method,order_amount,real_amount,is_reservation,'
                    . 'is_appoint,get_vali_code,a.mobile as user_mobile,d.dispatch_name,d.mobile as dispatch_mobile')
            ->join('db_app_users as a on a.user_id = o.user_id')
            ->join('db_dispatch as d on d.dispatch_id = o.dispatch_id')
            ->where($where)
            ->order("order_time desc")
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        $this->assign('total_count', $count);
        $this->assign("formget",$formget);
        $this->assign('lists', $lists);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }  
    /**
     * 充值明细
     */
    function water_list(){
        $water_db = M('user_recharge r');

        $mobile=  I("mobile");//支付账号
        $shop_name=  I("shop_name");//客户名称
        $start_time=  I("start_time");//开始时间
        $end_time=  I("end_time");//结束时间

        $formget=array('mobile'=>$mobile
                         ,'shop_name'=>$shop_name
                        ,'start_time'=>$start_time
                         , 'end_time'=>$end_time);

        $where = "1 ";

        if(!empty($mobile))
            $where = $where." and a.mobile like '%".$mobile."%'";
        if(!empty($shop_name))
            $where = $where." and s.shop_name like '%".$shop_name."%'";

        if(!empty($start_time))
            $where = $where." and r.pay_time >= '".$start_time." 00:00:00'";
        if(!empty($end_time))
            $where = $where." and r.pay_time <= '".$end_time." 23:59:59'";

        $count = $water_db->where($where)
                            ->join(' db_app_users a on a.user_id = r.user_id')
                          ->join(' left join db_user_shop s on s.shop_id = a.is_shop')->count();

        import("Org.Util.Pagemumayi");
        $page = new \Pagemumayi($count,20);
        //分页跳转的时候保证查询条件
        foreach($formget as $key=>$val) {
            $page->parameter .= "$key=".urlencode($val)."&";
        }

        $data = $water_db->field('ifnull(s.shop_name,a.mobile) as shop_name,r.user_id,a.mobile,r.pay_time,r.recharge_amount,r.give_money,recharge_record_id')
                            ->where($where)
                            ->join(' db_app_users a on a.user_id = r.user_id')
                            ->join(' left join db_user_shop s on s.shop_id = a.is_shop')
                            ->limit($page->firstRow.','.$page->listRows)
                            ->order(' pay_time desc')
                            ->select();

        $this->assign('datas',$data);
        $this->assign('page',$page->show('Admin'));
        $this->assign('formget',$formget);
        $this->display();
    }

    /**
     * 余额明细
     */
    function user_purse_list(){


        $utype=  I("utype");//电话
        $name=  I("name");//电话
        $mobile=  I("mobile");//电话
        $start_time=  I("start_time");//开始时间
        $end_time=  I("end_time");//结束时间
        $purse_type=  intval(I("purse_type"));//类型

        $formget=array('mobile'=>$mobile
                        ,'utype'=>$utype
                        ,'name'=>$name
                        ,'purse_type'=>$purse_type
                        ,'start_time'=>$start_time
                        , 'end_time'=>$end_time);

        $where = " 1=1 ";

        if($utype == 1 ){//用户
            if(!empty($name))
                $where = $where." and ifnull(s.shop_name,a.mobile) like '%".$name."%'";
            if(!empty($mobile))
                $where = $where." and a.mobile like '%".$mobile."%'";
            if(!empty($purse_type) && $purse_type!=-1)
                $where = $where.' and purse_type = '.$purse_type;
            if(!empty($start_time))
                $where = $where." and change_time >= '".$start_time." 00:00:00'";
            if(!empty($end_time))
                $where = $where." and change_time <= '".$end_time." 23:59:59'";

            $water_db = M('user_purse p');
            $count = $water_db->where($where)->join('db_app_users a ON p.user_id=a.user_id')
                ->join(' left join db_user_shop s on a.is_shop = s.shop_id')->count();
            import("Org.Util.Pagemumayi");
            $page = new \Pagemumayi($count,20);
            //分页跳转的时候保证查询条件
            foreach($formget as $key=>$val) {
                $page->parameter .= "$key=".urlencode($val)."&";
            }

//        $Current_page=1；
            $data = $water_db->field('ifnull(s.shop_name,a.mobile) as name,1 as utype,a.user_id as id,a.mobile,p.purse_amount as balance_amount, p.purse_type,p.purse_amount,p.change_time,p.change_remark,FORMAT(ifnull(p.sum_amount,0),2) as sum_amount')->join('db_app_users a ON p.user_id=a.user_id')
                ->join(' left join db_user_shop s on a.is_shop = s.shop_id')
                ->where($where)
                ->order(' change_time desc')
                ->limit($page->firstRow.','.$page->listRows)
                ->select();
           
            $this->assign('page',$page->show('Admin'));
        }elseif ($utype == 2 ){//跑客
            $dispatch_db = M('dispatch_balance b');
            if(!empty($name))
                $where = $where." and d.dispatch_name like '%".$name."%'";
            if(!empty($mobile))
                $where = $where." and d.mobile like '%".$mobile."%'";
            if(!empty($purse_type) && $purse_type!=-1)
                $where = $where.' and b.balance_type = '.$purse_type;
            if(!empty($start_time))
                $where = $where." and b.time >= '".$start_time." 00:00:00'";
            if(!empty($end_time))
                $where = $where." and b.time <= '".$end_time." 23:59:59'";

            $count = $dispatch_db->where($where)->join('db_dispatch d ON b.dispatch_id=d.dispatch_id')->count();
            import("Org.Util.Pagemumayi");
            $page = new \Pagemumayi($count,20);
            //分页跳转的时候保证查询条件
            foreach($formget as $key=>$val) {
                $page->parameter .= "$key=".urlencode($val)."&";
            }

            $data = $dispatch_db->field('d.dispatch_name as name,2 as utype,d.dispatch_id as id,d.mobile,b.balance_amount, b.balance_type as purse_type,d.cash_balance as purse_amount,b.time as change_time,b.remark as change_remark,FORMAT(ifnull(b.sum_amount,0),2) as sum_amount')->join('db_dispatch d ON b.dispatch_id=d.dispatch_id')
                ->where($where)
                ->order(' time desc')
                ->limit($page->firstRow.','.$page->listRows)
                ->select();

            $this->assign('page',$page->show('Admin'));
        }

        $this->assign('datas',$data);
//
        $this->assign("formget",$formget);
        $this->display();
    }
    
    /*
     * 提款明细
     */
    function withdrawal_details(){
      //所属团队(d.company_id),姓名(d.dispatch_name),电话(d.mobile),提款金额(c.cash_amount),提款时间(c.cash_time),提款状态(c.status),转款时间(c.pay_time),余额(b.balance_amount),说明(c.remark)
       $dispatch_cash = M('dispatch_cash c');
       $dispatch_balance = M('dispatch_balance b');
       $dispatch = M('dispatch d');
        
        $formget = I('formget');
        $dispatch_name = $formget['dispatch_name'];//姓名
        $mobile = $formget['mobile']; //电话
        $pay_time = $formget['pay_time']; //转款时间
                
        $formget = array('dispatch_name' => $dispatch_name,
                         'mobile' => $mobile,
                         'pay_time'=> $pay_time);
        
        $where = "1=1";
        if(!empty($dispatch_name))
            $where = $where." and dispatch_name like '%".$dispatch_name."%'";
        if(!empty($mobile))
            $where = $where." and d.mobile like '%".$mobile."%'";
        if(!empty($pay_time))
            $where = $where." and c.pay_time like '%".$pay_time."%'";
        
        $count = $dispatch_cash->where($where)
                                ->join('db_dispatch d on d.dispatch_id = c.dispatch_id')
                               ->count();
        //echo $dispatch_cash->getLastSql();
        //dump($count);
        import("Org.Util.Pagemumayi");
        $page = new \Pagemumayi($count,20);
        //分页跳转的时候保证查询条件
        foreach($formget as $key=>$val) {
            $page->parameter .= 'formget['."$key"."]=".urlencode($val)."&";
        }
        
        $lists = $dispatch_cash
                ->field('d.company_id,d.dispatch_name,d.mobile,c.cash_amount,c.cash_time,c.status,c.pay_time,c.remark,cash_record_id,c.dispatch_id')
                ->join('db_dispatch d on d.dispatch_id = c.dispatch_id')
                ->where($where)
                ->order('c.pay_time desc')
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
        //echo $dispatch_cash->getLastSql();
       // dump($lists);
//        die;
        $this->assign('total_count', $count);
        $this->assign("formget",$formget);
        $this->assign('lists', $lists);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }
   /*
    * 日单量
    */ 
    function order_amount(){
       //横轴日期 纵轴单量/额
       //(时间段、X用户/X跑客/团队)
       
        $formget = I('formget');

        $start_time = $formget['order_start_time'];
        $end_time = $formget['order_end_time'];
        $user_mobile = $formget['user_mobile']; //用户电话
        //dump($user_mobile);
        $dis_name = $formget['dis_name']; //跑客姓名
        $dis_mobile = $formget['dis_mobile'];//跑客电话
        $company = $formget['company_id'];  //跑客团队
         //接收商户ID
        $shop_id = I('shop_id');
        
        $where = " where pay_status =1 and `status` =7 ";

        if(!empty($shop_id))
        {
            $uid_sql = "select user_id from db_app_users where is_shop = ".$shop_id;
            $uid = M()->query($uid_sql);
            foreach ($uid as $k => $v)
            {
                $id[$k] = $v["user_id"]; 
            }
            $id = implode(",", $id);

            $where = $where. "and user_id in( ".$id." )";
        }
        if(!empty($start_time))
            $where = $where." and DATE_FORMAT(order_time, '%Y-%m-%d') >= '".$start_time." 00:00:00'";
        if(!empty($end_time))
                $where = $where." and DATE_FORMAT(order_time, '%Y-%m-%d') <= '".$end_time." 23:59:59' ";
        if(empty($start_time) && empty($end_time))
        {
                 $start_time = date('Y-m-d',time());
                 $end_time = date('Y-m-d',time());
                $where = $where." and DATE_FORMAT(order_time, '%Y-%m-%d') = date_format(now(), '%Y-%m-%d') ";
        }
        //用户电话
        if(!empty($user_mobile))
        {
            $app_users = M('app_users');
            $user_id = $app_users->where(array('mobile'=>$user_mobile))->getField('user_id');
            $where = $where."and user_id = ".$user_id;
        }
        //跑客姓名
        if(empty($dis_name) && empty($dis_mobile)){
            
        }else{
             $dis_where = "1";
        }
       
        if(!empty($dis_name))
        {
            $dis_where = $dis_where." and dispatch_name like '%".$dis_name."%'";
        }
        //跑客电话
        if(!empty($dis_mobile))
        {
            $dis_where = $dis_where." and mobile like '%".$dis_mobile."%'";
        }
        //跑客团队
    
        if($company != 1)
        {
            $where = $where." and company_id = ".$company;
        }
        if($dis_where)
        {
            $dispatch = M('dispatch');
            $dispatch_id = $dispatch->where($dis_where)->getField('dispatch_id');
            
            if($dispatch_id){
                $where = $where." and o.dispatch_id = ".$dispatch_id;
            }else{
                 $where = $where." and o.dispatch_id = null";
            }
          //  echo $dispatch->getLastSql();
        }
        
        $sql= "SELECT DATE_FORMAT(order_time, '%H') time_hour,count(order_id) as amount_order,sum(order_amount) as order_amount "
                . "from db_order  as o "
                . $where
                . " group by  DATE_FORMAT(order_time, '%H')";
        $data = M()->query($sql);
        //dump($data);
        if($data)
        {
            foreach($data as $key => $value)
            {
                $data2[$key] = $value["time_hour"]; 
                $count_order[$data2[$key]] = $value["amount_order"];
                $order_amount[$data2[$key]] = $value["order_amount"];
            }
          
            for($i=0; $i<=23;$i++)
            {
                foreach($count_order as $k => $v)
                {
                    if($i == $k)
                    {
                        $order[$i] = (int)$count_order[$k];
                        $money[$i] = (float)$order_amount[$k];
                        break;
                    }else{
                        $order[$i] = 0;
                        $money[$i] = 0;
                    }
                }
            }
            //处理数据,变成想要的格式
            $order[24]=$order[0];
            $money[24]=$money[0];
            unset($order[0]);
            unset($money[0]);

            $result_order = implode(",", $order);
            $result_order = "[".$result_order."]";
            
            $result_money = implode(",", $money);
            $result_money = "[".$result_money."]";
           
        }else{
            $result_order = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]";
            $result_money = "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]";
        }
       // dump($start_time);
         $formget =array('shop_id'          =>$shop_id
                        ,'order_end_time'   =>$end_time
                        ,'order_start_time' =>$start_time
                        ,'user_mobile'      =>$user_mobile 
                        ,'dis_name'         =>$dis_name
                        ,'dis_mobile'       =>$dis_mobile
                        ,'company_id'       =>$company);
        
        //获取所有大客户
        $UserShop = D('UserShop');
        $this->assign('user_shop',$UserShop->get_user_shop());
        
        $this->assign('result_order',$result_order);
        $this->assign('result_money',$result_money);
        $this->assign('formget',$formget);
        $this->assign('shop_id',$shop_id);
        $this->display();
    }
    /**
     * 数据分析--注册量
     */
    function register_echart(){
        //获取注册人数
        $formget = I('formget');
        $start_time= ($formget["start_time"]);//查询时间  注册用户
        $end_time= ($formget["end_time"]);
        $utype=  I("utype");//类别
        $company_id=  I("company_id");//类别

        if(!$start_time)
            $start_time = date('Y-m',time()).'-1';
        if(!$end_time)
            $end_time = date('Y-m-d',time());

        $formget= array(
            'start_time'=>$start_time
            ,'end_time'=>$end_time
            ,'utype'=>$utype
            ,'company_id'=>$company_id
        );
        $where = " 1 ";
        if(!empty($start_time)  )
            $where = $where." and regist_time >= '".$start_time." 00:00:00'";
        if(!empty($end_time) )
            $where = $where." and regist_time <= '".$end_time." 23:59:59'";

            $user_db = M('app_users a ');
            $user = $user_db->field("DATE_FORMAT(regist_time, '%Y-%m-%d') as regist_time,count(user_id) as regist_nums")->where($where)->group("DATE_FORMAT(regist_time, '%Y-%m-%d')")->order("DATE_FORMAT(regist_time, '%Y-%m-%d')")->select();
//            dump($user_db->getLastSql());

            $dis_db = M('dispatch d ');
            if($company_id && $company_id > 1){
                $where = $where." and company_id = ".$company_id;
            }
            $dispatchs = $dis_db->field("DATE_FORMAT(regist_time, '%Y-%m-%d') as regist_time,count(dispatch_id) as regist_nums")->where($where)->group("DATE_FORMAT(regist_time, '%Y-%m-%d')")->order("DATE_FORMAT(regist_time, '%Y-%m-%d')")->select();
//            dump($dis_db->getLastSql());

        if($user)
        {
            $user_str='';
            $date_str='';
            foreach ($user as $value){
                $user_str = $user_str.$value['regist_nums'].',';
                $date_str =  $date_str."'".$value['regist_time']."',";
            }


            $user_str = rtrim($user_str,',');
            $users = "[".$user_str."]";
            $date_str = rtrim($date_str,',');
            $dates = "[".$date_str."]";
        }else
        {
            $users ="[0]";
            $dates ="['".$start_time."']";
        }

        if($dispatchs)
        {
            $dis_str='';
            foreach ($dispatchs as $value){
                $dis_str = $dis_str.$value['regist_nums'].',';
            }


            $dis_str = rtrim($dis_str,',');
            $dispatch_str = "[".$dis_str."]";
        }else
        {
            $dispatch_str ="[0]";
        }

        $this->assign('users',$users);
        $this->assign('dispatchs',$dispatch_str);
        $this->assign('dates',$dates);
        $this->assign('company_data',M('company')->select());
        $this->assign('formget',$formget);
        $this->display();
    }
    function get_company_data(){
        if (IS_AJAX) {
            $ret = M('company')->order('company_id')->select();
            if ($ret) {
                RET(0, "成功", $ret);
            } else {
                RET_NO_V(1, "失败");
            }
        }
    }

    /**
     * 地图显示发单量页面
     *  order_id 订单id
     */
    function bill_order_map(){
        $this->display();
    }
    /**
     * 地图显示发单量
     */
    function bill_order(){
        $formget = I('formget');
        $start_time= ($formget["start_time"]);//查询时间  注册用户
        $end_time= ($formget["end_time"]);

        $formget= array(
            'start_time'=>$start_time
        ,'end_time'=>$end_time
        );
        $where = " 1 ";
        if(!empty($start_time)  )
            $where = $where." and finish_time >= '".$start_time." 00:00:00'";
        if(!empty($end_time) )
            $where = $where." and finish_time <= '".$end_time." 23:59:59'";

//       [ {"lng":116.419787,"lat":39.930658,"count":15},{"lng":116.418455,"lat":39.920921,"count":40}]

        $order_lng_lat = M('order o')->field('case when order_type =3 then o.reci_longit else o.send_longit  end as lng'.
			                ',case when order_type =3 then o.reci_latit else o.send_latit end as lat'.
			                ',count(o.order_id) as count')->where($where)
                    ->group('case when order_type =3 then o.reci_longit else o.send_longit  end,case when order_type =3 then o.reci_latit else o.send_latit end')
                    ->order('case when order_type =3 then o.reci_longit else o.send_longit  end,case when order_type =3 then o.reci_latit else o.send_latit end')
                    ->select();
//        dump($order_lng_lat);
        $order_lng_lat_json = json_encode($order_lng_lat);
//        dump($order_lng_lat_json);

        $this->assign('datas',$order_lng_lat_json);
        $this->assign('formget',$formget);
        if($order_lng_lat_json){
            echo $order_lng_lat_json;
        }
    }

    /*
    * 订单量/额
    */ 
        function order_form(){
       //横轴日期 纵轴单量/额
       //(时间段、X用户/X跑客/团队)
        $formget = I('formget');
        $st_time = $formget['order_start_time'];
        $en_time = $formget['order_end_time'];
        
        $start_time = $formget['order_start_time'];
        $end_time = $formget['order_end_time'];        
        $user_mobile = $formget['user_mobile']; //用户电话
        $dis_name = $formget['dis_name']; //跑客姓名
        $dis_mobile = $formget['dis_mobile'];//跑客电话
        $company = $formget['company_id'];  //跑客团队
        //接收商户ID
        $shop_id = I('shop_id');
        
        $where = " where pay_status =1 and `status` =7 ";
        
         if(!empty($shop_id))
        {
            $uid_sql = "select user_id from db_app_users where is_shop = ".$shop_id;
            $uid = M()->query($uid_sql);
            foreach ($uid as $k => $v)
            {
                $id[$k] = $v["user_id"]; 
            }
            $id = implode(",", $id);

            $where = $where. "and user_id in( ".$id." )";
        }
        
        //用户电话
        if(!empty($user_mobile))
        {
            $app_users = M('app_users');
            $user_id = $app_users->where(array('mobile'=>$user_mobile))->getField('user_id');
            $where = $where."and user_id = ".$user_id." ";
        }
        //跑客姓名
        if(empty($dis_name) && empty($dis_mobile)){
            
        }else{
             $dis_where = "1";
        }
       
        if(!empty($dis_name))
        {
            $dis_where = $dis_where." and dispatch_name like '%".$dis_name."%'";
        }
        //跑客电话
        if(!empty($dis_mobile))
        {
            $dis_where = $dis_where." and mobile like '%".$dis_mobile."%'";
        }
        //跑客团队
    
        if($company != 1)
        {
            $where = $where." and company_id = ".$company." ";
        }
        if($dis_where)
        {
            $dispatch = M('dispatch');
            $dispatch_id = $dispatch->where($dis_where)->getField('dispatch_id');
            
            if($dispatch_id){
                $where = $where." and o.dispatch_id = ".$dispatch_id;
            }else{
                 $where = $where." and o.dispatch_id = null ";
            }
          //  echo $dispatch->getLastSql();
        }
//         if(!empty($start_time))
//            $where = $where." and DATE_FORMAT(order_time, '%Y-%m-%d') >= '".$start_time." 00:00:00'";
//        if(!empty($end_time))
//            $where = $where." and DATE_FORMAT(order_time, '%Y-%m-%d') <= '".$end_time." 23:59:59' ";
        if(empty($start_time) && empty($end_time))
        {
                //获得当前年月日的时间戳
                $end_time = strtotime(date('Y-m-d',time()));
                $en_time = date('Y-m-d',time());
                
                //从当前时间向前推21天的时间戳
                $start_time = $end_time-(60*60*24*21); 
                $st_time = date('Y-m-d',$start_time);
                
                while($start_time <= $end_time){
                    $time_day[] = date("Y-m-d",$start_time);
                    $start_time = $start_time +(60*60*24); 
                }
                foreach($time_day as $key => $value){
                    $where_time =  "and order_time like '%".$value."%'";
                    $sql = "SELECT DATE_FORMAT(order_time, '%Y-%m-%d') as time_day,"
                            . "count(order_id) as amount_order,sum(order_amount) as order_amount "
                            . "from db_order  as o "
                            . $where.$where_time
                            . " group by  DATE_FORMAT(order_time, '%e') "
                            . "order by order_time";
                   // dump($sql);
                    $date = M()->query($sql);
                    if($date){
                        $result_date[$key] = $date;
                    }else{
                        $result_date[$key][0]["time_day"] = $value;
                        $result_date[$key][0]["amount_order"] = 0;
                        $result_date[$key][0]["order_amount"] = 0;
                    }
                }    
        }else{
            $start_time = $formget['order_start_time'];
            $end_time = $formget['order_end_time'];
            $start_time = strtotime($start_time);
            $end_time = strtotime($end_time);
            while($start_time <= $end_time){
              $time_day[] = date("Y-m-d",$start_time);
              $start_time = $start_time +(60*60*24); 
            }
            foreach($time_day as $key => $value){
                    $where_time =  "and order_time like '%".$value."%'";
                    $sql = "SELECT DATE_FORMAT(order_time, '%Y-%m-%d') as time_day,"
                            . "count(order_id) as amount_order,sum(order_amount) as order_amount "
                            . "from db_order  as o "
                            . $where.$where_time
                            . " group by  DATE_FORMAT(order_time, '%e') "
                            . "order by order_time"; 
                   // dump($sql);
                    $date = M()->query($sql);
                    if($date){
                        $result_date[$key] = $date;
                    }else{
                        $result_date[$key][0]["time_day"] = $value;
                        $result_date[$key][0]["amount_order"] = 0;
                        $result_date[$key][0]["order_amount"] = 0;
                    }
                }             
        }
            $sum_money = 0;
            $sum_order = 0;
        foreach($result_date as $k => $v){
            $result_day[] = '"'.$v[0]["time_day"].'"';
            $result_order[] = $v[0]["amount_order"];
            $result_money[] = $v[0]["order_amount"];
            //dump($v);
            $sum_money += $v[0]["order_amount"];
            $sum_order += $v[0]["amount_order"];
        }
//        dump($sum_money);
//        dump($sum_order);
        $data = '["订单总量:'.$sum_order.'","订单总金额:'.$sum_money.'"]';
//        dump($data);
        $result_day = implode(",", $result_day);
        $result_day = "[".$result_day."]";

        $result_order = implode(",", $result_order);
        $result_order = "[".$result_order."]";
            
        $result_money = implode(",", $result_money);
        $result_money = "[".$result_money."]";
        
        $formget =array('shop_id'=>$shop_id,
             'order_end_time'    =>$en_time
            ,'order_start_time'  =>$st_time
            ,'user_mobile'       =>$user_mobile 
            ,'dis_name'          =>$dis_name
            ,'dis_mobile'        =>$dis_mobile
            ,'company_id'        =>$company);
        
        //获取所有大客户
        $UserShop = D('UserShop');
        $this->assign('user_shop',$UserShop->get_user_shop());
        //dump($data);
        $this->assign('data',$data);
        $this->assign('result_day',$result_day);
        $this->assign('result_order',$result_order);
        $this->assign('result_money',$result_money);
        $this->assign('sum_money',$sum_money);
        $this->assign('sum_order',$sum_order);
        $this->assign('formget',$formget);
        $this->assign('shop_id',$shop_id);
        
        $this->display();
    }
    /**
     * 数据分析--下单里程图
     */
    function order_mileage_echart(){
        //获取注册人数

        $formget = I('formget');
        $user_id=  $formget["user_id"];//大客户
        $user_name=  $formget["user_name"];//用户名
        $mobile=  $formget["mobile"];//电话

        $start_time = $formget['start_time'];
        $end_time = $formget['end_time'];


        if(!$start_time)
            $start_time = date('Y-m',time()).'-1';
        if(!$end_time)
            $end_time = date('Y-m-d',time());

        $formget= array( 'user_id'=>$user_id,
            'user_name'=>$user_name,
            'mobile'=>$mobile,
            'start_time'=>$start_time
            ,'end_time'=>$end_time
        );

        $where = " o.`status` = 7 ";
        if(!empty($user_name))
            $where = $where." and a.user_name like '%".$user_name."%'";
        if(!empty($mobile))
            $where = $where." and a.mobile like '%".$mobile."%'";
        if(!empty($user_id) && $user_id > 0 )
            $where = $where." and a.is_shop = ".$user_id;

        if(!empty($start_time)  )
            $where = $where." and finish_time >= '".$start_time." 00:00:00'";
        if(!empty($end_time) )
            $where = $where." and finish_time <= '".$end_time." 23:59:59'";

        $order_db = M('order o');
        $order_counts = $order_db->field("ifnull(ROUND(o.total_mileage),0) as total_mileage,count(order_id) as order_count")
                        ->join(' db_app_users a on a.user_id = o.user_id')
                        ->where($where)->group("ifnull(ROUND(o.total_mileage),0)")->order("ifnull(ROUND(o.total_mileage),0)")->select();
//         dump($order_db->getLastSql());

        if($order_counts)
        {
            $order_counts_str='';
            $mileage_str='';
            foreach ($order_counts as $value){
                $order_counts_str = $order_counts_str.$value['order_count'].',';
                $mileage_str =  $mileage_str."'".$value['total_mileage']."公里',";
            }
            $order_counts_str = rtrim($order_counts_str,',');
            $order_counts_str = "[".$order_counts_str."]";
            $mileage_str = rtrim($mileage_str,',');
            $mileage_str = "[".$mileage_str."]";
        }else
        {
            $order_counts_str ="[0]";
            $mileage_str ="[0]";
        }
//        dump($order_counts_str);
//        dump($mileage_str);
        $this->assign('order_counts',$order_counts_str);
        $this->assign('mileages',$mileage_str);
        $this->assign('user_shop',M('user_shop')->order('sort')->select());
        $this->assign('formget',$formget);
        $this->display();
    }
}

